package vip.zhenzicheng.nettyadv.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import vip.zhenzicheng.nettyadv.cryocodec.KryoDecoder;
import vip.zhenzicheng.nettyadv.cryocodec.KryoEncoder;

/**
 * 客户端Handler的初始化
 *
 * @author zhenzicheng
 * @date 2022-06-14 10:09
 */
public class ClientInit extends ChannelInitializer<SocketChannel> {

  @Override
  protected void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));

    /*连接写空闲检测*/
    ch.pipeline().addLast(new CheckWriteIdleHandler());
    /*粘包半包*/
    ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2));
    ch.pipeline().addLast(new LengthFieldPrepender(2));

    /*序列化相关*/
    ch.pipeline().addLast(new KryoDecoder());
    ch.pipeline().addLast(new KryoEncoder());

    ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
    ch.pipeline().addLast(new LoginAuthReqHandler());

    /*连接读空闲检测*/
    ch.pipeline().addLast(new ReadTimeoutHandler(15));
    /*向服务器发出心跳请求*/
    ch.pipeline().addLast(new HeartBeatReqHandler());

    ch.pipeline().addLast(new ClientBusyHandler());

  }
}
